// pages/search/search.js
import request from '../../utils/request'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    searchvalue:'',//输入的值
    hotsearchlist:'',//热搜列表
    searchsuggestlist:[],//建议搜索
    searchhistorylist:[],//搜索历史
    searchsonglist:[],//搜索歌曲列表
    searchDefault:'陈奕迅',//默认搜索
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.hotsearchlist();
    this.getsearchhistorylist();
  },
  //热搜列表
  hotsearchlist(){
    request('/search/hot/detail')
    .then(res => {
      console.log(res.data)
      this.setData({
        hotsearchlist:res.data
      })
    })
  },
  //建议搜索
  inputchange(event){
    // console.log(event.detail.value)
    if (event.detail.value.trim()!=''){
      this.setData({
        searchvalue: event.detail.value
      })
    }else{
      this.setData({
        searchvalue: '',
        searchsuggestlist:[],
      })
      clearTimeout(timer);
      return;
      
    }


    //清除定时器
    clearTimeout(timer)
    let timer = setTimeout(()=>{
      this.search();
    },500)


   
  },
  //搜索
  search(){
    let { searchvalue} = this.data;
    if (searchvalue){
      request('/search', { keywords: searchvalue, limit: 10 })
        .then(res => {
          console.log(res.result.songs)
       

          this.setData({
            searchsuggestlist: res.result.songs,
         
          })

        })
    }
    
  },
  //跳转到播放音乐
  playmusic(event) {
    let { songid, index } = event.currentTarget.dataset;
    wx.setStorageSync('playingMusicList', this.data.searchsonglist);
    wx.navigateTo({
      url: '/pages/songdetail/songdetail?songid=' + songid,
      success: (res) => {
        // 通过eventChannel向被打开页面传送数据,这个data名字是你自己取的任意,在目标页面中有个参数接收就可以
        res.eventChannel.emit('parentPageEmit', { data: index });

      },

    })
  },
  //热门搜索事件
  hotsearchitem(e){
    
    let searchword = e.currentTarget.dataset.searchword
    this.setData({
      searchvalue: searchword
    })
    //调用搜索回车事件
    this.searchSubmit();
  },
  //历史点击事件
  searchhistoryclick(e){
  let item = e.currentTarget.dataset.item
    this.setData({
      searchvalue: item
    })
    //调用搜索回车事件
    this.searchSubmit();
  },
  //建议搜索点击事件
  suggestsearch(e){
    let name = e.currentTarget.dataset.name
    this.setData({
      searchvalue: name
    })
    //调用搜索回车事件
    this.searchSubmit();
  },

  //获取历史记录
  getsearchhistorylist(){
    let searchhistorylist = wx.getStorageSync('searchhistorylist')
    if(searchhistorylist){
      this.setData({
        searchhistorylist
      })
    }

    
  },
  //input回车事件
  searchSubmit(e){
  
    let { searchvalue, searchhistorylist, searchsonglist, searchDefault } = this.data;
    if (!searchvalue){
      searchvalue = searchDefault
      this.setData({
        searchvalue
      })
    }
    request('/cloudsearch', { keywords: searchvalue })
      .then(res => {
        console.log(res);
        searchsonglist = res.result.songs
        this.setData({
          searchsonglist,
          searchsuggestlist: []
        })
      })
    

    // console.log(e.detail.value)
    //判断历史列表里有没有搜索过
    if (searchhistorylist.indexOf(searchvalue) !== -1) {
      //删除历史搜索过的
      searchhistorylist.splice(searchhistorylist.indexOf(searchvalue), 1)
    }
    //向历史列表添加数据
    searchhistorylist.unshift(searchvalue)
    wx.setStorageSync('searchhistorylist', searchhistorylist)

    this.setData({
      searchhistorylist
    })
    
  },
  //清空搜索宽的值
  clearsearchvalue(){
    //找到历史搜索列表与input输入框里面的相同的值删除掉，并且删除本地缓存的值,
    // let { searchvalue, searchhistorylist} = this.data;
    // //判断历史列表里有没有搜索过
    // if (searchhistorylist.indexOf(searchvalue) !== -1) {
    //   //删除历史搜索过的
    //   searchhistorylist.splice(searchhistorylist.indexOf(searchvalue), 1)
    // }


    this.setData({
      searchvalue:'',
      searchsuggestlist:[],
      searchsonglist:[]
      // searchhistorylist,
    })
    // wx.setStorageSync('searchhistorylist', searchhistorylist)
  },
  //删除搜索历史
  deletehistorylist(){
    wx.showModal({
      title: '提示删除',
      content: '你确认删除吗',
      success:(res) => {
        if (res.confirm){
        this.setData({
          searchhistorylist:[],
        });
          wx.removeStorageSync('searchhistorylist');
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})